package org.xdq.demo.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.xdq.demo.dto.GoodsDto;
import org.xdq.demo.dto.Result;
import org.xdq.demo.service.GoodsService;

@RestController
public class GoodsController {
	
	@Autowired
	private GoodsService goodsService;
	
	@GetMapping("/goods") 
	public Map<String,Object> goodsList(GoodsDto dto) {
		return goodsService.getGoodsList(dto);
	}
	
	@PostMapping("/goods")
	public Result goodsExecAdd(@RequestBody GoodsDto dto) {
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		goodsService.addNewGoods(dto);
		return Result.successResult("新增商品成功！");
	}
	
	@PutMapping("/goods")
	public Result goodsExecEdit(@RequestBody GoodsDto dto) {
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		goodsService.editGoods(dto);
		return Result.successResult("编辑商品成功！");
	}
	
	
	@RequestMapping(value="/goods",method= {RequestMethod.DELETE})
	public Result goodsExecDelete(@RequestBody Integer[] gids) {
		
		goodsService.deleteGoods(gids);
		return Result.successResult("删除商品成功！");
	}
	
	@RequestMapping(value="/goods/{g_id}",method= {RequestMethod.DELETE})
	public Result goodsExecDelete(@PathVariable Integer g_id) {
		
		goodsService.deleteGoods(g_id);
		return Result.successResult("删除商品成功！");
	}

}
